import { RequestData } from "@ant-design/pro-table";
import { TenantItem, TenantUpdateItem, UpdateTenantPwdBean } from "./data";
import { ResponsePaginationData, SUCCESS_CODE } from "@/utils/request";
import { request } from "umi";
import { message } from "antd";

/**
 * 客户获取客户列表
 * @param params
 */
export async function queryTenantListByTenant(params: {
  page?: number,
  pageSize?: number,
}
): Promise<RequestData<TenantItem>> {
  try {
    const res: ResponsePaginationData<TenantItem> = await request('/tenant/view', {
      params: {
        page: 1,
        pageSize: 10,
        ...params,
      },
    });
    if (res?.code !== SUCCESS_CODE) {
      return { success: false, total: 0, data: [] };
    }
    return {
      success: true,
      total: res.data.total,
      data: res.data.list,
    };
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: [] };
  }
}

/**
 * 修改客户密码
 * @param data 
 * @returns 
 */
export async function updateTenantPwd(data: UpdateTenantPwdBean) {
  try {
    const res = await request('/tenant/update_pwd', {
      method: 'POST',
      data: data,
    });
    if (!res || res.code !== SUCCESS_CODE) {
      message.error(res.message);
    }
    return res;
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: null };
  }
}

/**
 * 修改客户信息
 * @param data
 */
export async function updateTenant(data: TenantUpdateItem) {
  try {
    const res = await request('/tenant/update', {
      method: 'POST',
      data: data,
    });
    if (!res || res.code !== SUCCESS_CODE) {
      message.error(res.message);
    }
    return res;
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: null };
  }
}
